<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>The SplObjectStorage class</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="splfixedarray.wakeup.html">« SplFixedArray::__wakeup</a></li>
      <li style="float: right;"><a href="splobjectstorage.addall.html">SplObjectStorage::addAll »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="spl.datastructures.html">数据结构</a></li>
    <li>The SplObjectStorage class</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="class.splobjectstorage" class="reference">

 <h1 class="title">The SplObjectStorage class</h1>
 

 <div class="partintro"><p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8)</p>


  <div class="section" id="splobjectstorage.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    The SplObjectStorage class provides a map from objects to data or, by
    ignoring data, an object set. This dual purpose can be useful in many
    cases involving the need to uniquely identify objects.
   </p>
  </div>


  <div class="section" id="splobjectstorage.synopsis">
   <h2 class="title">类摘要</h2>


   <div class="classsynopsis">
    <div class="ooclass">
     
    </div>

    <div class="classsynopsisinfo">
     <span class="ooclass">
      <span class="modifier">class</span> <strong class="classname">SplObjectStorage</strong>
     </span>

     <span class="oointerface"><span class="modifier">implements</span> 
       <a href="class.countable.html" class="interfacename">Countable</a></span><span class="oointerface">,  <a href="class.iterator.html" class="interfacename">Iterator</a></span><span class="oointerface">,  <a href="class.serializable.html" class="interfacename">Serializable</a></span><span class="oointerface">,  <a href="class.arrayaccess.html" class="interfacename">ArrayAccess</a></span> {</div>

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="splobjectstorage.addall.html" class="methodname">addAll</a></span>(<span class="methodparam"><span class="type"><a href="class.splobjectstorage.html" class="type SplObjectStorage">SplObjectStorage</a></span> <code class="parameter">$storage</code></span>): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splobjectstorage.attach.html" class="methodname">attach</a></span>(<span class="methodparam"><span class="type">object</span> <code class="parameter">$object</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$info</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splobjectstorage.contains.html" class="methodname">contains</a></span>(<span class="methodparam"><span class="type">object</span> <code class="parameter">$object</code></span>): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splobjectstorage.count.html" class="methodname">count</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$mode</code><span class="initializer"> = <strong><code>COUNT_NORMAL</code></strong></span></span>): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splobjectstorage.current.html" class="methodname">current</a></span>(): <span class="type">object</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splobjectstorage.detach.html" class="methodname">detach</a></span>(<span class="methodparam"><span class="type">object</span> <code class="parameter">$object</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splobjectstorage.gethash.html" class="methodname">getHash</a></span>(<span class="methodparam"><span class="type">object</span> <code class="parameter">$object</code></span>): <span class="type">string</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splobjectstorage.getinfo.html" class="methodname">getInfo</a></span>(): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splobjectstorage.key.html" class="methodname">key</a></span>(): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splobjectstorage.next.html" class="methodname">next</a></span>(): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splobjectstorage.offsetexists.html" class="methodname">offsetExists</a></span>(<span class="methodparam"><span class="type">object</span> <code class="parameter">$object</code></span>): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splobjectstorage.offsetget.html" class="methodname">offsetGet</a></span>(<span class="methodparam"><span class="type">object</span> <code class="parameter">$object</code></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splobjectstorage.offsetset.html" class="methodname">offsetSet</a></span>(<span class="methodparam"><span class="type">object</span> <code class="parameter">$object</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$info</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splobjectstorage.offsetunset.html" class="methodname">offsetUnset</a></span>(<span class="methodparam"><span class="type">object</span> <code class="parameter">$object</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splobjectstorage.removeall.html" class="methodname">removeAll</a></span>(<span class="methodparam"><span class="type"><a href="class.splobjectstorage.html" class="type SplObjectStorage">SplObjectStorage</a></span> <code class="parameter">$storage</code></span>): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splobjectstorage.removeallexcept.html" class="methodname">removeAllExcept</a></span>(<span class="methodparam"><span class="type"><a href="class.splobjectstorage.html" class="type SplObjectStorage">SplObjectStorage</a></span> <code class="parameter">$storage</code></span>): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splobjectstorage.rewind.html" class="methodname">rewind</a></span>(): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splobjectstorage.serialize.html" class="methodname">serialize</a></span>(): <span class="type">string</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splobjectstorage.setinfo.html" class="methodname">setInfo</a></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$info</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splobjectstorage.unserialize.html" class="methodname">unserialize</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$data</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="splobjectstorage.valid.html" class="methodname">valid</a></span>(): <span class="type">bool</span></div>

   }</div>


  </div>


  <div class="section" id="splobjectstorage.examples">
   <h2 class="title">范例</h2>
   <p class="para">
    <div class="example" id="example-3552">
     <p><strong>示例 #1 <span class="classname"><strong class="classname">SplObjectStorage</strong></span> as a set</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;As&nbsp;an&nbsp;object&nbsp;set<br /></span><span style="color: #0000BB">$s&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SplObjectStorage</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$o1&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">StdClass</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$o2&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">StdClass</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$o3&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">StdClass</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$s</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">attach</span><span style="color: #007700">(</span><span style="color: #0000BB">$o1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$s</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">attach</span><span style="color: #007700">(</span><span style="color: #0000BB">$o2</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$s</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">contains</span><span style="color: #007700">(</span><span style="color: #0000BB">$o1</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$s</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">contains</span><span style="color: #007700">(</span><span style="color: #0000BB">$o2</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$s</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">contains</span><span style="color: #007700">(</span><span style="color: #0000BB">$o3</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">$s</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">detach</span><span style="color: #007700">(</span><span style="color: #0000BB">$o2</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$s</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">contains</span><span style="color: #007700">(</span><span style="color: #0000BB">$o1</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$s</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">contains</span><span style="color: #007700">(</span><span style="color: #0000BB">$o2</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$s</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">contains</span><span style="color: #007700">(</span><span style="color: #0000BB">$o3</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
bool(true)
bool(true)
bool(false)
bool(true)
bool(false)
bool(false)
</pre></div>
     </div>
    </div>
    <div class="example" id="example-3553">
     <p><strong>示例 #2 <span class="classname"><strong class="classname">SplObjectStorage</strong></span> as a map</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;As&nbsp;a&nbsp;map&nbsp;from&nbsp;objects&nbsp;to&nbsp;data<br /></span><span style="color: #0000BB">$s&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SplObjectStorage</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$o1&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">StdClass</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$o2&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">StdClass</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$o3&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">StdClass</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$s</span><span style="color: #007700">[</span><span style="color: #0000BB">$o1</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">"data&nbsp;for&nbsp;object&nbsp;1"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$s</span><span style="color: #007700">[</span><span style="color: #0000BB">$o2</span><span style="color: #007700">]&nbsp;=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,</span><span style="color: #0000BB">2</span><span style="color: #007700">,</span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /><br />if&nbsp;(isset(</span><span style="color: #0000BB">$s</span><span style="color: #007700">[</span><span style="color: #0000BB">$o2</span><span style="color: #007700">]))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$s</span><span style="color: #007700">[</span><span style="color: #0000BB">$o2</span><span style="color: #007700">]);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(3) {
  [0]=&gt;
  int(1)
  [1]=&gt;
  int(2)
  [2]=&gt;
  int(3)
}
</pre></div>
     </div>
    </div>
   </p>
  </div>


 </div>

 


































































































































<h2>目录</h2><ul class="chunklist chunklist_reference"><li><a href="splobjectstorage.addall.html">SplObjectStorage::addAll</a> — Adds all objects from another storage</li><li><a href="splobjectstorage.attach.html">SplObjectStorage::attach</a> — Adds an object in the storage</li><li><a href="splobjectstorage.contains.html">SplObjectStorage::contains</a> — Checks if the storage contains a specific object</li><li><a href="splobjectstorage.count.html">SplObjectStorage::count</a> — Returns the number of objects in the storage</li><li><a href="splobjectstorage.current.html">SplObjectStorage::current</a> — Returns the current storage entry</li><li><a href="splobjectstorage.detach.html">SplObjectStorage::detach</a> — Removes an object from the storage</li><li><a href="splobjectstorage.gethash.html">SplObjectStorage::getHash</a> — Calculate a unique identifier for the contained objects</li><li><a href="splobjectstorage.getinfo.html">SplObjectStorage::getInfo</a> — Returns the data associated with the current iterator entry</li><li><a href="splobjectstorage.key.html">SplObjectStorage::key</a> — Returns the index at which the iterator currently is</li><li><a href="splobjectstorage.next.html">SplObjectStorage::next</a> — Move to the next entry</li><li><a href="splobjectstorage.offsetexists.html">SplObjectStorage::offsetExists</a> — Checks whether an object exists in the storage</li><li><a href="splobjectstorage.offsetget.html">SplObjectStorage::offsetGet</a> — Returns the data associated with an object</li><li><a href="splobjectstorage.offsetset.html">SplObjectStorage::offsetSet</a> — Associates data to an object in the storage</li><li><a href="splobjectstorage.offsetunset.html">SplObjectStorage::offsetUnset</a> — Removes an object from the storage</li><li><a href="splobjectstorage.removeall.html">SplObjectStorage::removeAll</a> — Removes objects contained in another storage from the current storage</li><li><a href="splobjectstorage.removeallexcept.html">SplObjectStorage::removeAllExcept</a> — Removes all objects except for those contained in another storage from the current storage</li><li><a href="splobjectstorage.rewind.html">SplObjectStorage::rewind</a> — Rewind the iterator to the first storage element</li><li><a href="splobjectstorage.serialize.html">SplObjectStorage::serialize</a> — Serializes the storage</li><li><a href="splobjectstorage.setinfo.html">SplObjectStorage::setInfo</a> — Sets the data associated with the current iterator entry</li><li><a href="splobjectstorage.unserialize.html">SplObjectStorage::unserialize</a> — Unserializes a storage from its string representation</li><li><a href="splobjectstorage.valid.html">SplObjectStorage::valid</a> — Returns if the current iterator entry is valid</li></ul>
</div>
</div></div></body></html>